package refect.jsontest;

import com.alibaba.fastjson.JSON;
import org.junit.Test;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class TestJson {

    @Test
    public void test1() {
        Person p = new Person();
        p.setName("张三");
        p.setAge(18);
        p.setList(Arrays.asList("a","b","c"));
        // 实体类转化为json
        String personStr = JSON.toJSONString(p);
        // json转换为map
        Map map = JSON.parseObject(personStr, Map.class);
        List<String> list = (List<String>) map.get("list");
        System.out.println(list);
    }
    @Test
    public void test() {
        List<User> users = Arrays.asList(
                new User("张三", 18, "河南"),
                new User("李四", 28, "郑州"),
                new User("王五", 38, "洛阳"));
        String usersStr = JSON.toJSONString(users);

        List<Map> maps = JSON.parseArray(usersStr, Map.class);
        System.out.println(maps);
    }
    @Test
    public void test3() {
        List<User> users = Arrays.asList(
                new User("张三", 18, "河南"),
                new User("李四", 28, "郑州"),
                new User("王五", 38, "洛阳"));
        String usersStr = JSON.toJSONString(users);

//        List<User> parse = (List<User>) JSON.parse(usersStr);
        List<Map> parse = (List<Map>) JSON.parse(usersStr);
        System.out.println(parse);
    }
}
